//
//  ArticleCell.m
//  Final
//
//  Created by student14 on 2021/12/14.
//  Copyright © 2021 Lenrony. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ArticleCell.h"
#import "RawUserView.h"

@interface ArticleCell()

@property (strong, nonatomic) Article *article;
@property (strong, nonatomic) UIViewController *svc;

@property (strong ,nonatomic) UILabel *contentLabel; // 当前Cell中的文本内容
@property (strong, nonatomic) UILabel *postTimeLabel; // 表示帖子发布的时间
@property (strong, nonatomic) UILabel *fieldLabel; // 表示帖子发布的分区
@property (strong, nonatomic) RawUserView *ruv; // 发帖用户的缩略信息

@end

@implementation ArticleCell

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    [self setBackgroundColor:[UIColor whiteColor]];
    [self initSubview];
    
    return self;
}

- (void)initSubview {
    _svc = nil;
    _article = [[Article alloc] initWithUID:@"00000000" andAID:@"" andField:0 andContent:@"" andImages:nil andTypes:nil andTimeStamp:@"xxxx-xx-xx xx:xx:xx"];
    
    _ruv = [[RawUserView alloc] initWithRawUser:[[RawUser alloc] initWithUID:@"00000000" andName:@"Anonymous" andHead:[UIImage imageNamed:@"renwu.png" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil]] andViewController:_svc];
    [_ruv setFrame:CGRectMake(5, 5, 80, 40)];
    [self addSubview:_ruv];
    
    _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, self.ruv.frame.origin.y+self.ruv.frame.size.height+5, [UIScreen mainScreen].bounds.size.width-60, 0)];
    _contentLabel.textColor = [UIColor blackColor];
    [_contentLabel setNumberOfLines:0];
    [_contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [_contentLabel setFont:[UIFont systemFontOfSize:20]];
    _contentLabel.text = _article.content;
    [_contentLabel sizeToFit];
    [self addSubview:_contentLabel];
    
    _fieldLabel = [[UILabel alloc] init];
    _fieldLabel.textColor = [UIColor blueColor];
    [_fieldLabel setFont:[UIFont systemFontOfSize:12]];
    _fieldLabel.text = @"";
    _fieldLabel.frame = CGRectMake(30, 5+_contentLabel.frame.origin.y+_contentLabel.frame.size.height, 100, 28);
    [self addSubview:_fieldLabel];
    
    _postTimeLabel = [[UILabel alloc] init];
    _postTimeLabel.textColor = [UIColor grayColor];
    [_postTimeLabel setFont:[UIFont systemFontOfSize:12]];
    _postTimeLabel.text = _article.time_stamp;
    _postTimeLabel.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-150, 5+_contentLabel.frame.origin.y+_contentLabel.frame.size.height, 150, 28);
    [self addSubview:_postTimeLabel];
}

- (void)setArticle:(Article *)article
        andRawUser:(RawUser *)ru
 andViewController:(UIViewController *)svc
{
    _article = article;
    _svc = svc;
    
    if (_ruv == nil) {
        _ruv = [[RawUserView alloc] initWithRawUser:ru andViewController:_svc];
    }
    else {
        [self.ruv resetRawuser:ru andViewController:svc];
    }
    
    _contentLabel.frame = CGRectMake(30, self.ruv.frame.origin.y+self.ruv.frame.size.height+5, [UIScreen mainScreen].bounds.size.width-60, 0);
    [_contentLabel setNumberOfLines:0];
    [_contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
    //    _contentLabel.text = [article.content stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
    _contentLabel.text = article.content;
    [_contentLabel sizeToFit];
    
    switch (article.field) {
        case 1:
            _fieldLabel.text = @"#二手闲置#";
            break;
        case 2:
            _fieldLabel.text = @"#打听求助#";
            break;
        case 3:
            _fieldLabel.text = @"#青青子衿#";
            break;
        case 4:
            _fieldLabel.text = @"#瓜田趣事#";
            break;
        case 5:
            _fieldLabel.text = @"#校园招聘#";
            break;
        default:
            break;
    }
    _fieldLabel.frame = CGRectMake(30, 5+_contentLabel.frame.origin.y+_contentLabel.frame.size.height, 100, 28);
    
    _postTimeLabel.text = article.time_stamp;
    _postTimeLabel.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-150, 5+_contentLabel.frame.origin.y+_contentLabel.frame.size.height, 150, 28);
}

@end
